package com.prp.db.daoimpl.mysql;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.prp.db.idao.IUserInformationDAO;
import com.prp.db.po.UserInformation;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserInfomation entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.prp.db.daoimpl.mysql.UserInfomation
 * @author MyEclipse Persistence Tools
 */

public class UserInformationDAO extends BaseHibernateDAO implements
		IUserInformationDAO {
	private static final Log log = LogFactory.getLog(UserInformationDAO.class);
	// property constants
	public static final String USER_REG_NAME = "userRegName";
	public static final String USER_REAL_NAME = "userRealName";
	public static final String USER_CLASS_ID = "userClassId";
	public static final String USER_GRADE_ID = "userGradeId";
	public static final String GENDER = "gender";
	public static final String AGE = "age";
	public static final String EMAIL = "email";
	public static final String PASSWORD = "password";
	public static final String PICTURE_ROUTINE = "pictureRoutine";
	public static final String IDENTITY = "identity";
	public static final String COMMUNITY_ID = "communityId";
	public static final String ACTIVATED = "activated";

	public void save(UserInformation transientInstance) {
		log.debug("saving UserInformation instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(UserInformation persistentInstance) {
		log.debug("deleting UserInformation instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserInformation findById(java.lang.Integer id) {
		log.debug("getting UserInformation instance with id: " + id);
		try {
			UserInformation instance = (UserInformation) getSession().get(
					UserInformation.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(UserInformation instance) {
		log.debug("finding UserInformation instance by example");
		try {
			List results = getSession().createCriteria(
					"com.prp.db.daoimpl.mysql.UserInfomation").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserInformation instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = null;
			if (null != value)
				queryString = "from UserInformation as model where model."
						+ propertyName + "= ?";
			else
				queryString = "from UserInformation as model where model."
						+ propertyName + " is null";
			Query queryObject = getSession().createQuery(queryString);
			if (null != value)
				queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserRegName(Object userRegName) {
		return findByProperty(USER_REG_NAME, userRegName);
	}

	public List findByUserRealName(Object userRealName) {
		return findByProperty(USER_REAL_NAME, userRealName);
	}

	public List findByUserClassId(Object userClassId) {
		return findByProperty(USER_CLASS_ID, userClassId);
	}

	public List findByUserGradeId(Object userGradeId) {
		return findByProperty(USER_GRADE_ID, userGradeId);
	}

	public List findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List findByAge(Object age) {
		return findByProperty(AGE, age);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByPictureRoutine(Object pictureRoutine) {
		return findByProperty(PICTURE_ROUTINE, pictureRoutine);
	}

	public List findByIdentity(Object identity) {
		return findByProperty(IDENTITY, identity);
	}

	public List findByCommunityId(Integer communityId) {
		return findByProperty(COMMUNITY_ID, communityId);
	}

	public List findAll() {
		log.debug("finding all UserInfomation instances");
		try {
			String queryString = "from UserInformation";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public UserInformation merge(UserInformation detachedInstance) {
		log.debug("merging UserInformation instance");
		try {
			UserInformation result = (UserInformation) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(UserInformation instance) {
		log.debug("attaching dirty UserInfomation instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UserInformation instance) {
		log.debug("attaching clean UserInformation instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public int getCommunitySize(int community_id) {
		Object result = getSession().createCriteria(UserInformation.class).add(
				Restrictions.eq(COMMUNITY_ID, community_id)).setProjection(
				Projections.count(EMAIL)).uniqueResult();
		if (result == null)
			return -1;
		return (Integer) result;
	}

	public List<String> findStringByEmailPattern(String query) {
		log.debug("find string by email pattern");
		try {
			String queryString = "select email from UserInformation as model where model.email like ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, query);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByActivated(boolean activated) {
		return findByProperty(ACTIVATED, activated);
	}

	@Override
	public List<String> findStringByNamePattern(String query) {
		log.debug("find string by name pattern");
		try {
			String userNameQuery = "select userRegName from UserInformation as model where model.userRegName like ?";
			String realNameQuery = "select userRealName from UserInformation as model where model.userRealName like ?";
			Query userNameQueryObject = getSession().createQuery(userNameQuery);
			userNameQueryObject.setParameter(0, query);
			Query realNameQueryObject = getSession().createQuery(realNameQuery);
			realNameQueryObject.setParameter(0, query);
			List<String> list = userNameQueryObject.list();
			list.addAll(realNameQueryObject.list());
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List<UserInformation> findByNamePattern(String query) {
		log.debug("find by name pattern");
		try {
			String userNameQuery = "from UserInformation as model where model.userRegName like ?" +
					" or model.userRealName like ?";
			Query queryObject = getSession().createQuery(userNameQuery);
			queryObject.setParameter(0, query);
			queryObject.setParameter(1, query);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List<UserInformation> findByUserNamePattern(String query) {
		log.debug("find by name pattern");
		try {
			String userNameQuery = "from UserInformation as model where model.userRegName like ?";
			Query queryObject = getSession().createQuery(userNameQuery);
			queryObject.setParameter(0, query);
			queryObject.setParameter(1, query);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
}